package com.ocom.exposeapi.controller;

import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.ExposeApiUrl;
import com.ocom.common.entity.master.EntityCompanyList;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.company.GetSeverReplyApiParaRequest;
import com.ocom.common.request.exposeapi.PlatformLoginAuthRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.SignUtils;
import com.ocom.common.vo.company.SrvCodeVo;
import com.ocom.exposeapi.feign.CompanyClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;

/**
 * @Description: 平台页面code跳转
 * @Author: 朱晖
 * @Date: 2023/5/8
 **/
@Slf4j
@RestController
public class PlatformController {


	@Autowired
	private CompanyClientService companyClientService;


	//4.3.1登录验证（platform#loginAuth）
	@PostMapping(ExposeApiUrl.URL_ExposeApi_platformLoginAuth)
	public Result loginAuth(@Validated @RequestBody PlatformLoginAuthRequest request, BindingResult bindingResult) {

		log.info("执行平台链接1 【{}】",request);

		GetSeverReplyApiParaRequest getSeverReplyApiParaRequest = new GetSeverReplyApiParaRequest();
		getSeverReplyApiParaRequest.setSvrAppid(request.getSrvId());   //srvappidlegengkeji
		getSeverReplyApiParaRequest.setDockCode("platform");
		getSeverReplyApiParaRequest.setDockApiCode("platform#getAuthCode");
		getSeverReplyApiParaRequest.setKeyType(0);
		Result result = companyClientService.getSeverReplyApiPara(getSeverReplyApiParaRequest);
		log.info("执行平台链接2 【{}】【{}】",getSeverReplyApiParaRequest,result);

		if (result.getCode() != 200) {
			return result;
		}


		SrvCodeVo srvCodeVo = JSON.parseObject(JSON.toJSONString(result.getData()), SrvCodeVo.class);

		String sign = request.getSign().toLowerCase();
		request.setSign("");
		SortedMap<String, Object> parameters = JSON.parseObject(JSON.toJSONString(request), SortedMap.class);
		String sign1 = SignUtils.createSign(parameters, srvCodeVo.getKey());
		System.out.println(sign1);
		if (!sign.equals(sign1)) {
			return ResultUtil.error(ResultEnum.Err_202);
		}

		Result result1 = companyClientService.comLoginAuth("platform#dzbp", request.getAuthCode());


		log.info("执行平台链接3 【{}】",result1);


		if (result1.getCode() != 200) {
			return result1;
		}

		Map<String,Object> resMap =new HashMap<>();
		resMap = JSON.parseObject(JSON.toJSONString(result1.getData()),HashMap.class);
	//	http://file.ocomtraffic.com/img-library/452604d57b8c434d1c034c25ed1767c.png

		resMap.put("logo","http://file.ocomtraffic.com/daily/2023/05/11/452604d57b8c434d1c034c25ed1767c.png");
		resMap.put("comName","");

		Result result2  = companyClientService.GetCompanyListInfoOne(srvCodeVo.getComId());

		if (result2.getCode() == 200) {
			EntityCompanyList entityCompanyList  = JSON.parseObject(JSON.toJSONString(result2.getData()),EntityCompanyList.class);
			resMap.put("comName",entityCompanyList.getComName());
			if(!CommonUtil.isNull(entityCompanyList.getLogoUrl())){
				resMap.put("logo",entityCompanyList.getLogoUrl());
			}
		}

		return ResultUtil.success(resMap);
	}


}
